package com.hjx.servlet;



import com.hjx.pojo.Order;
import com.hjx.service.orderService;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

public class addOrderServlet extends HttpServlet {
    private orderService order=null;
    private List<Order> orderList=null;
    private ServletContext context=null;
    private HttpSession session=null;
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        session=req.getSession();
        order=new orderService();
        context=req.getServletContext();
        String product_name=req.getParameter("product_name");
        String supplier=req.getParameter("supplier");
        String order_money=req.getParameter("order_money");
        String payStatus= req.getParameter("payStatus");
        String coding=req.getParameter("coding");
        String createTime=getTime();

        String[] args={
                coding,product_name,supplier,order_money,payStatus,createTime
        };
        try{
            int count=order.insertOrder(args);
            if (count>0){
                System.out.println("订单插入成功！");
//                将页面转发到后台管理页面   转发时不能加上下文路径 /super
                req.getRequestDispatcher("/Page/index.jsp").forward(req,resp);
//                将成功状态放到servletContext中
                session.setAttribute("status","success");
            }else{
                session.setAttribute("status","error");
                System.out.println("订单插入失败！");
//                转发时不能加上下文路径 /super
                req.getRequestDispatcher("/Page/index.jsp").forward(req,resp);
            }

        }catch (Exception e){
            e.printStackTrace();
        }

    }

    public String getTime(){
        String time="";
        //        System.currentTimeMillis():时间戳
        SimpleDateFormat formatter= new SimpleDateFormat("yyyy-MM-dd 'at' HH:mm:ss z");
        Date date = new Date(System.currentTimeMillis());
        time=formatter.format(date);
        return time;
    }


    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

    }
}
